package com.oyjd.business.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.oyjd.R;
import com.oyjd.business.baidu.activity.BaiduPosDemo;
import com.oyjd.business.condsel.activity.CondSelActivity;
import com.oyjd.business.handler.activity.HandlerDemo;
import com.oyjd.business.listview.activity.ListViewDemo;
import com.oyjd.business.menu.activity.MenuBarDemo;
import com.oyjd.business.movepage.activity.ViewPageDemo;
import com.oyjd.business.photo.activity.GalleryActivity;
import com.oyjd.business.reply.activity.ReplyDemo;
import com.oyjd.business.set.activity.SettingActivity;
import com.oyjd.business.tab.activity.TabsDemo;
import com.oyjd.framework.util.MsgShow;

/**
 * GridView使用例子
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class GridViewDemo extends Activity {
	private LayoutInflater flater;// 布局加载器
	private List<View> views = new ArrayList<View>();// 子视图

	/**
	 * 初始化图标数据
	 * 
	 * @return
	 */
	private List<IconVO> getData() {
		List<IconVO> list = new ArrayList<IconVO>();
		list.add(new IconVO(R.drawable.mt_fen_dou, "列表示例", ListViewDemo.class));
		list.add(new IconVO(R.drawable.mt_leng_hang, "百度定位示例", BaiduPosDemo.class));
		list.add(new IconVO(R.drawable.mt_wei_xiao, "Handler示例", HandlerDemo.class));
		list.add(new IconVO(R.drawable.mt_fa_yun, "拍照回复示例", ReplyDemo.class));
		list.add(new IconVO(R.drawable.mt_chi_jing, "菜单示例", MenuBarDemo.class));
		list.add(new IconVO(R.drawable.mt_hai_pa, "Tabs示例", TabsDemo.class));
		list.add(new IconVO(R.drawable.mt_da_ku, "左右滑动视图", ViewPageDemo.class));
		list.add(new IconVO(R.drawable.mt_da_xiao, "设置示例", SettingActivity.class));
		list.add(new IconVO(R.drawable.mt_du_zhui, "翻页动画", GalleryActivity.class));
		list.add(new IconVO(R.drawable.mt_an_heng, "模拟浏览器", WebViewDemo.class));
		list.add(new IconVO(R.drawable.mt_hai_xiu, "滑动选条件", CondSelActivity.class));
		return list;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_gridview);

		// 初始化布局加载器
		flater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 初始化子视图
		List<IconVO> datas = getData();
		for (IconVO vo : datas) {
			views.add(createItemView(vo));
		}
		GridViewInnerAdapter adapter = new GridViewInnerAdapter();
		//
		GridView gridview = (GridView) this.findViewById(R.id.gridview);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				IconVO vo = (IconVO) arg1.getTag();
				Intent intent = new Intent(GridViewDemo.this, vo.cls);
				startActivity(intent);
			}
		});

	}

	/**
	 * 内部自定义适配器
	 * 
	 * @author 欧建东
	 * @email oyjd@163.com
	 * 
	 */
	private class GridViewInnerAdapter extends BaseAdapter {

		public int getCount() {
			return views.size();
		}

		public Object getItem(int position) {
			return views.get(position).getTag();
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return views.get(position);
		}
	}

	/**
	 * 根据数据创建视图
	 * 
	 * @param vo
	 * @return
	 */
	private View createItemView(IconVO vo) {
		View view = flater.inflate(R.layout.grid_item_demo, null);
		((TextView) view.findViewById(R.id.gv_tv_txt)).setText(vo.iconText);
		((ImageView) view.findViewById(R.id.gv_img_pic)).setImageResource(vo.iconImage);
		view.setTag(vo);
		return view;
	}

	private class IconVO {
		public int iconImage;
		public String iconText;
		public Class<?> cls;

		public IconVO(int iconImage, String iconText, Class<?> cls) {
			this.iconImage = iconImage;
			this.iconText = iconText;
			this.cls = cls;
		}
	}

	// 用于监听手机的退出事件，彻底退出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MsgShow.showExitApp(GridViewDemo.this);
		}
		return false;
	}

}
